package cc.imos.web;

import cc.imos.communal.entity.*;
import cc.imos.communal.err.ErrCode;
import cc.imos.communal.err.ErrException;
import cc.imos.communal.kit.WebKit;
import cc.imos.entity.SysAccount;
import cc.imos.entity.SysAccountLine;
import cc.imos.medium.AccountPageArgs;
import cc.imos.medium.LoginRest;
import cc.imos.medium.LoginArgs;
import cc.imos.service.SystemAccountService;
import com.xiaoleilu.hutool.crypto.digest.DigestUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <pre>
 *     账号控制器
 * Created by asher on 2017/3/4.
 * </pre>
 */
@Slf4j
@RestController
@Api(tags = {"SystemAccountWeb"}, description = "账号接口")
@RequestMapping(path = {"/system/account"}, method = {RequestMethod.POST})
public class SystemAccountWeb {

    @Autowired
    private SystemAccountService systemAccountService;

    @RequestMapping( path = {"/login"})
    @ApiOperation(value = "登录接口", notes = "登录系统接口")
    public Rest<LoginRest> login(
            @ApiParam(value = "登录参数", required = true)
            @RequestBody @Valid LoginArgs loginArgs, BindingResult bindingResult) {
        //
        Rest<LoginRest> rest = new Rest<>();
        try {
            WebKit.hasErrors(bindingResult);

            loginArgs.setPassword(DigestUtil.md5Hex(loginArgs.getPassword()));
            LoginRest loginRest = systemAccountService.login(loginArgs);
            rest.setBody(loginRest);
        } catch (ErrException e) {
            rest.set(e);
        }
        return rest;
    }

    /**
     * 登出方法
     *
     * @param accessToken 账号令牌
     *
     * @return 响应数据
     */
    @RequestMapping(path = {"/logout"})
    @ApiOperation(value = "登出接口", notes = "登出系统接口")
    public Rest<Object> logout(
            @ApiParam(value = "账号令牌", required = true)
            @RequestHeader String accessToken) {
        Rest<Object> rest = new Rest<>();
        try {
            if(StringUtils.hasText(accessToken)) {
                SysAccountLine sysAccountLine = new SysAccountLine();
                sysAccountLine.setAccessToken(accessToken);
                systemAccountService.logout(sysAccountLine);
            } else {
                rest.set(ErrCode.xxx);
            }
        } catch (ErrException e) {
            rest.set(e);
        }
        return rest;
    }

    @RequestMapping( path = {"/page"})
    @ApiOperation(value = "浏览账号", notes = "分页浏览账号接口")
    public Rest<Page<SysAccount>> page(
            @ApiParam(value = "参数", required = false)
            @RequestBody @Valid AccountPageArgs pageArgs, BindingResult bindingResult) {
        Rest<Page<SysAccount>> rest = new Rest();
        try {
            WebKit.hasErrors(bindingResult);

        } catch (ErrException e) {
            rest.set(e);
        }

        return rest;
    }

}
